package inter_demo

import (
	"fmt"
	"reflect"
)

type Db interface {
	Insert(any) error
}

type Mongo struct {
	charset string
}

func (m *Mongo) Insert(data any) error {
	s, ok := data.(string)
	if ok {
		fmt.Println("Mongo insert:", s)
	} else {
		fmt.Println("非字符串类型数据，插入失败")
	}
	return nil
}

func AntTest() {
	var d Db
	m := Mongo{"utf-8"}
	fmt.Println(d, m)
}

func dealData(data any) {
	//类型断言
	s, ok := data.(string)
	//当ok为true时候，断言成立，s为string类型的变量
	if ok {
		fmt.Println("data为字符串类型:", s, reflect.TypeOf(s))
	} else {
		fmt.Println("非字符串类型")
		//断言不成立，s没有数据
		fmt.Println(s)
	}
}

func dataSelect(data any) {
	switch t := data.(type) {
	case int:
		fmt.Println("int:", t, reflect.TypeOf(t))
	case string:
		fmt.Println("string:", t, reflect.TypeOf(t))
	case bool:
		fmt.Println("bool:", t, reflect.TypeOf(t))
	default:
		fmt.Println("未知类型")
	}
}

func DealDemo() {
	dealData("123")
	dealData(12)

	dataSelect("123")
	dataSelect(32)
	dataSelect(1.2)
}

type User interface {
	Login()
	Logout()
}

type VipUser interface {
	User
	TelService()
}

type UserImpl struct {
	Name string
}

func (u UserImpl) Login() {
	fmt.Println("登录:", u.Name)
}

func (u UserImpl) Logout() {
	fmt.Println("退出:", u.Name)
}

type VipUserImpl struct {
	UserImpl
	Level string
}

func (u VipUserImpl) TelService() {
	fmt.Println("电话服务:", u.Name, u.Level)
}

func InterComboDemo() {
	var u User
	us := UserImpl{"acy"}
	u = us
	u.Login()
	u.Logout()

	vus := VipUserImpl{us, "10"}
	var vu VipUser = vus
	vu.Login()
	vu.Logout()
	vu.TelService()
}
